package com.xteam.xnetworkdisk.sys.controller;

import com.xteam.xnetworkdisk.utils.ImageCaptchaUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

// 获取图片验证码
@RestController
@RequestMapping("/getImageCaptcha")
public class ImageController {

    @GetMapping
    public void getImageCaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        String captcha = ImageCaptchaUtil.generateRandomCode(4);
        BufferedImage bufferedImage = ImageCaptchaUtil.generateCaptchaImage(captcha);
        ImageIO.write(bufferedImage, "png", response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
        session.setAttribute("captcha", captcha);
    }
}
